**************************************************************************************************
*** This do file creates the spatial lag for 
*** Authors in alphabetical order: Denise Laroze, Eric Neumayer, Thomas Pluemper.
*** Covid-19 does not Stop at Open Borders: Spatial Contagion among Local Authority Districts During England’s First Wave 
*** Social Science & Medicine, Volume 270, February 2021, 113655, https://doi.org/10.1016/j.socscimed.2020.113655. 

*** This is for illustration only. The spatial lag variable is already contained in the replication dataset
							
**************************************************************************************************
**************************************************************************************************
/* Note: You have to change "local DIR" to the directory you copy the original stata files contained 	*/
/* in the zip file and then run the do file. 
/* You must have Stata version 13 or higher installed													*/
**************************************************************************************************
*/

drop _all
clear matrix
clear mata


capture net install outreg2, from(http://fmwww.bc.edu/RePEc/bocode/o) /* checks whether outreg2 is installed 		*/

capture net install spmon, from(http://fmwww.bc.edu/RePEc/bocode/s) /* checks whether spmon is installed 		*/

***********************************************************************************
local DIR = "D:\Research\Health for all database\United Kingdom\"  /*change relative path to the directory where the files are located */
cd "`DIR'"
***********************************************************************************


use "Article for Social Science & Medicine (C19) spatial source data.dta", clear

 set more off, perm
 
sum commuters_total

capture dropvars comm_tot_fraction
gen comm_tot_fraction=commuters_total/r(max) 
label var  comm_tot_fraction "=commuters_total/r(max)"

 
capture dropvars pop_100
gen pop_100=population_2018_k/100000
label var pop_100 "=population_2018_k/100000"

* population
spmon  pop_100, w(comm_tot_fraction) i(code_i) k(code_k) sename(pop_100_tot_comm) filename (pop_100_tot_comm) norow time(week)


* cases
spmon  w_cases_k, w(comm_tot_fraction) i(code_i) k(code_k) sename( cases_w_tot_comm) filename ( cases_w_tot_comm) norow time(week)


* cases per 100,000
gen cases100_w_splag_comm=cases_w_tot_comm/pop_100_tot_comm


collapse cases100_w_splag_comm, by(code_i week)

**** Descriptives test
sum *_splag_*


decode code_i, gen(code_i_string)


ren week Week_number

save "spatial_lag_w_commuters.dta", replace


